<script setup>
import {wechatAppletLoginAPI, wechatAppLoginAPI} from "../../services/login";
import {conUtils} from "../../utils/conUtils";
import {getUserInfoAPI} from "../../services/me";

const authLogin = async (type = 0) => {
  await uni.showLoading({
    title: '正在登录',
    mask: true,
  })
  // #ifdef APP-PLUS
  uni.login({
    "provider": "weixin",
    "onlyAuthorize": true, // 微信登录仅请求授权认证
    success: async function (loginRes) {
      const res = await wechatAppLoginAPI(loginRes.code)
      await setInfo(res, type)
    },
    fail: (err) => {
      uni.hideLoading()
      uni.showToast({
        title: '登录失败',
        icon: 'none',
        duration: 1500,
        mask: true,
      })
      conUtils(err)
    }
  })
  // #endif
  // #ifdef MP-WEIXIN
  uni.getUserProfile({
    desc: '登录',
    success: (userInfos) => {
      conUtils('用户信息', userInfos)
      uni.login({
        provider: 'weixin',
        success: async function (login_res) {
          uni.hideLoading()
          conUtils("getUserProfile", userInfos)
          conUtils("login", login_res)
          const res = await wechatAppletLoginAPI(login_res.code, userInfos.userInfo.avatarUrl, userInfos.userInfo.nickName)
          conUtils('res.result.token', res.result.token)
          conUtils('loginbind----res', res)
          await setInfo(res, type)
        },
      })
    },
    fail: (err) => {
      uni.hideLoading()
      uni.showToast({
        title: '登录失败',
        icon: 'none',
        duration: 1500,
        mask: true,
      })
      conUtils(err)
    },
  })
  // #endif
}

const setInfo = async (res, type) => {
  setProfile(res.result)
  await uni.showToast({
    title: res.msg,
    icon: 'none',
    duration: 1500,
    mask: true,
  })
  if (type === 1) {
    return
  }
  const resUser = await getUserInfoAPI()
  setUserInfo(resUser.result)
  setTimeout(async () => {
    await uni.navigateBack()
    // if (!resUser.result.username) {
    //   await uni.navigateTo({
    //     url: "/pages/me/settinginfo"
    //   })
    // }
  }, 1000)
}
</script>

<template>

</template>

<style scoped lang="scss">

</style>